package com.woniuxy.test_exception;

/**
 * description: Test04 <br>
 * date: 2022/2/2 5:43 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 自定义异常
 */
public class Test04 {
    public static void main(String[] args) {
        Person p = new Person();
        p.setAge(-1);
    }
}

class Person {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 0) {
            try {
                throw new IllegaAgeException("年龄不能为负数！");
            } catch (IllegaAgeException e) {
                e.printStackTrace();
            }
        }
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {

        this.name = name;
    }
}

class IllegaAgeException extends Exception {
    public IllegaAgeException() {
    }

    public IllegaAgeException(String msg) {
        super(msg);
    }
}